<?php
namespace Home\Controller;
use Think\Controller;
use Home\Model\AccountDetailsModel;


/**
 * 账户明细类
 */
class AccountDetailsController extends UserBaseController
{

	public $diffDay = 1;

	function __construct(){
		parent::__construct();
		$this->Account = new AccountDetailsModel();
	}

	/**
	 * 账户管理
	 * @param  int $offset 从第几天数据开始
	 * @param  int $limit  数据条数
	 * @param  string $date 日期
	 * @param  int $status 账户明细类型，0-全部记录/全部流水，1-收入记录/收入，2-支出记录/提现
	 * @param int $member_id 成员id
	 * @return array    二维数组
	 */
	public function account_user($offset = 0,$limit = 10,$date = FALSE,$status = FALSE){
		$income_list = $this->Account->get_account_details($offset,null,false,$this->uid);
		$user_db  = M('user');
		$user_data = $user_db->where(array('uid'=>$this->uid))->find();
		$money = 0;
		$withdraw_money = 0;
		$now_time = time();
		foreach ($income_list as $key => $income) {
			$diff_time =  $now_time - $this->diffDay*86400;
			if($income['a_date']<$diff_time && $income['a_status']==1){
				$money += $income['a_money'];
			}

			if($income['a_status']==2){
				$money -= $income['a_money'];
			}
		}

		$account_details_list = $this->Account->get_account_details($offset,$limit,$date,$this->uid,$status);
		foreach ($account_details_list as $key => $account_details) {
			$account_details_list[$key]['a_date'] = date('Y-m-d H:i',$account_details['a_date']);
//			if($account_details['a_status']==2){
//				$withdraw_money += $account_details['a_money'];
//			}
		}

		$data['m_money'] = $user_data['money']-$withdraw_money;
		$data['available_money'] = $money;
		$data['account_details'] = $account_details_list;

		returnMobileJson(200,'获取成功',$data);
	}


	/**
	 * 返回可提现金额
	 * @return int
	 */
	private function withdraw_money(){
		$income_list = $this->Account->get_account_details(0,null,false,$this->uid);
		$money = 0;
		$now_time = time();
		foreach ($income_list as $key => $income) {
			$diff_time =  $now_time - $this->diffDay*86400;
			if($income['a_date']<$diff_time && $income['a_status']==1){
				$money += $income['a_money'];
			}
			if($income['a_status']==2){
				$money -= $income['a_money'];
			}
		}

		return $money;
	}

	/**
	 * 添加账户明细
	 * @param array $data 账户明细数据z
	 */
	public function add_account_details(){
		$a_money=$_POST['a_money'];
		$a_status=2;

		if($a_money<1){
			returnMobileJson(-2,'提现金额需大于1元');
		}

		$withdraw_money = $this->withdraw_money();
		if($a_money>$withdraw_money){
			returnMobileJson(202,'可提现金额不足');
		}

		$user_db  = M('user');
		$user_data = $user_db->where(array('uid'=>$this->uid))->find();

//		if(!$member['openid']){
//			returnMobileJson(202,'请绑定公众号后再申请提现');
//		}

		$map = array();
		$map['uid'] = $this->uid;
		$saveData = array();
		$saveData['money'] = $user_data['money'] - $a_money;

		$user_db->where($map)->save($saveData);

		if( empty($a_money) || !isset($a_status) )
			returnMobileJson(201,'参数错误');
		$data=array('a_money'=>$a_money, 'a_status'=> $a_status,'a_date' => time(), 'uid'=>$this->uid);

		if($this->Account->set_account_details($data))
			returnMobileJson(200,'申请提现');
		else
			returnMobileJson(202,'添加失败');
	}

	/**
	 * 获取提现数据列表
	 * @param  int $offset 从第几天数据开始
	 * @param  int $limit  数据条数
	 * @param  string $date      日期
	 * @param  int $type 成员类型，0-全部，1-城市合伙人，2-联盟商户，3-自有工程师，4-滴滴工程师，5-联盟商户工程师，6-联盟商户门店
	 * @return json 提现数据列表
	 */
	public function get_withdraw_cash($offset = 0,$limit = 10,$date = FALSE,$type = FALSE){
		$Member = new MemberModel();
		if($type==2){
			// 联盟商户工程师列表
			$withdraw_cash_list1 = $this->Account->withdraw_cash_list($offset,$limit,$date,5);
			// 联盟商户门店
			$withdraw_cash_list2 = $this->Account->withdraw_cash_list($offset,$limit,$date,6);
			$withdraw_cash_list = array_merge($account_details_list1,$account_details_list2);
		}else{
			$withdraw_cash_list = $this->Account->withdraw_cash_list($offset,$limit,$date,$type);
		}
		foreach ($withdraw_cash_list as $key => $withdraw_cash) {
			switch ($account_details['m_type']) {
				case '1':
					$withdraw_cash_list[$key]['member'] = '城市合伙人';
					break;
				case '3':
					$withdraw_cash_list[$key]['member'] = '自有工程师';
					break;
				case '4':
					$withdraw_cash_list[$key]['member'] = '滴滴工程师';
					break;
				case '5':
				case '6':
					$withdraw_cash_list[$key]['member'] = '联盟商户';
					break;
				default:
					$withdraw_cash_list[$key]['member'] = '城市合伙人';
					break;
			}
			unset($withdraw_cash_list[$key]['m_type']);
			$withdraw_cash_list[$key]['a_date'] = date('Y-m-d H:i',strtotime($withdraw_cash['a_date']));
		}
		if(empty($withdraw_cash_list))
			returnMobileJson(201,'没有数据');
		else
			returnMobileJson(200,'获取成功',$withdraw_cash_list);
	}

	/**
	 * 通过审核提现
	 */
	public function audit_withdraw_cash(){
		$aid = $_POST['aid'];
		if(empty($aid)){
			returnMobileJson(201,'参数错误');
		}
		$data['a_audit_status'] = 1;
		if($this->Account->set_account_details($data,$aid)){
			returnMobileJson(200,'操作成功');
		}else{
			returnMobileJson(202,'操作失败');
		}
	}

	public function export_excel($date = FALSE,$status = FALSE,$offset = 0,$limit = null){
        //引入PHPExcel库文件
        Vendor('PHPExcel.PHPExcel');
        //创建对象
        $excel = new \PHPExcel();
        //Excel表格式,这里简略写了8列
        $letter = array('A','B','C','D','E','F','F','G');
        //表头数组
        $tableheader = array('序号','收支金额','收支项目','时间');
        //填充表头信息
        for($i = 0;$i < count($tableheader);$i++) {
            $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
        }
        //从数据库获取数据作为表格数组
        $account_details_list = $this->Account->get_account_details($offset,$limit,$date,$this->member_id,$status);
        foreach ($account_details_list as $key => $account_details) {
        	$data[$key][0] = $key+1;
        	if($account_details['a_status']==1){
        		$data[$key][1] = '+'.$account_details['a_money'].'元';
        		$data[$key][2] = '订单收入   订单编号：'.$account_details['order_num'];
        	}else{
        		$data[$key][1] = '-'.$account_details['a_money'].'元';
        		$data[$key][2] = '提现操作';
        	}
        	$data[$key][4] = $account_details['a_date'];
        }

        //填充表格信息
        for ($i = 2;$i <= count($data) + 1;$i++) {
            $j = 0;
            foreach ($data[$i - 2] as $key=>$value) {
                $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
                $j++;
            }
        }
        //创建Excel输入对象
        $write = new \PHPExcel_Writer_Excel5($excel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename="Go机网账户明细_'.date('YmdHis').'.xls"');
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');
    }

}